package com.struts.utils;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/2/9.
 */
public class Struts_xml {
    public Struts_xml() {
    }

    public  static  Map<String,XmlBeans> add()throws Exception{
        SAXBuilder saxBuilder = new SAXBuilder();
        Document document = saxBuilder.build(new File("E:\\ITEM\\demo\\demo_struts\\web\\WEB-INF/struts-config.xml"));
        //拿到根节点
        Element element =document.getRootElement();
      /*  //拿到子节点
        Element elementForm = element.getChild("form-beans");
        //拿到孙子节点
        List<Element> beanForm = elementForm.getChildren();
        //循环子节点里面的所有
        for (Element e : beanForm){

            String s = e.getAttributeValue("name");
            String s1 = e.getAttributeValue("type");
        }*/

        Map<String,XmlBeans> rmap = new HashMap<String, XmlBeans>();
        Element elementroot = element.getChild("action-mappings");
        String path="";
        List<Element> action = elementroot.getChildren();
        for (Element e : action){
            XmlBeans xmlBeans  = new XmlBeans();
            String name1 = e.getAttributeValue("name");
            xmlBeans.setBeanName(name1);
            Element elementForm = element.getChild("form-beans");
            List<Element> from = elementForm.getChildren();
            for (Element e1 :from){
                if (name1.equals(e1.getAttributeValue("name"))){

                    String fromClass = e1.getAttributeValue("class");
                    xmlBeans.setFromClass(fromClass);
                    break;
                }
            }
             path = e.getAttributeValue("path");
            xmlBeans.setPath(path);
            String type = e.getAttributeValue("type");
            xmlBeans.setActionClass(type);
            List<Element> actionfword = e.getChildren();
            Map<String,String> map = new HashMap<String, String>();
            for (Element el: actionfword){

                String name2 = el.getAttributeValue("name");
                String value = el.getAttributeValue("value");
                map.put(name2,value);
            }
            xmlBeans.setActionForword(map);
            rmap.put(path,xmlBeans);
        }


        return  rmap;
    }
}
